<?php

/**
* alltosun.com 优惠券前台控制器 inde.php
* ============================================================================
* 版权所有 (C) 2009-2014 北京互动阳光科技有限公司，并保留所有权利。
* 网站地址: http://www.alltosun.com
* ----------------------------------------------------------------------------
* 许可声明: 这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
* ============================================================================
* $Author: 吴松键 (wusj@alltosun.com) $
* $Date: 2014-9-15 下午12:23:59 $
* $Id$
*/

class Action
{
    private $user_id;

    public function __construct()
    {
        checkLogin(get_url());
        $this->user_id = user_helper::get_user_id();
    }

    /**
     * 显示优惠券列表
     */
    public function __call($action = '', $param = array())
    {
        Response::display('coupon.html');
    }

    public function index()
    {
        Response::display('coupon_list.html');
    }

    /**
     * 领取优惠券  暂未使用
     */
    public function receive()
    {
        $coupon_code = AnFilter::filter_string(Request::getParam('code', ''));
        if (!$this->user_id) {
            return '请先登录';
        }
        if (!$coupon_code) {
            return '重新领取';
        }

        $info = coupon_helper::check_is_available($coupon_code);
        if ($info['info'] != 'ok') {
            return $info['info'];
        }
        if ($info['status'] != 1) {
            return '当前优惠券已经被领取了';
        }

        //是否已经领取过
        $filter['coupon_id'] = _uri('coupon_relation', array('coupon_code'=>$coupon_code), 'coupon_id');
        $filter['user_id'] = $this->user_id;
        $coupon_relation_info = _uri('coupon_relation', $filter);
        if (!empty($coupon_relation_info)) {
            return '当前账户已经领取过了，请不要重复领取';
        }

        _model('coupon_relation')->update(array('coupon_code'=>$coupon_code), array('user_id'=>$this->user_id, 'status'=>2));
    }

    /**
     * 绑定优惠券
     */
    public function bind_coupon()
    {
        $coupon_code = Request::getParam('coupon_code', '');
        if (!$this->user_id) {
            return '请先登录';
        }
        if (!$coupon_code) {
            return '请填写优惠编码';
        }

        $info = coupon_helper::check_is_available($coupon_code);
        if ($info['info'] != 'ok') {
            return $info['info'];
        }
        if ($info['status'] != 1) {
            return '当前优惠券已经被领取了';
        }
        if($info['is_all_user']) {
            return '此优惠卷不允许绑定';
        }

        $coupon_relation_info = _uri('coupon_relation', array('coupon_code'=>$coupon_code));
        if (!empty($coupon_relation_info['user_id'])) {
            return '当前优惠券已经被领取了';
        }

        _model('coupon_relation')->update($coupon_relation_info['id'], array('user_id'=>$this->user_id, 'status'=>2));

        $coupon_info = _uri('coupon', $coupon_relation_info['coupon_id']);

        return array(
                'info'   => 'ok',
                'title'  => $coupon_info['title'],
                'price'  => $coupon_info['offer_price'],
                'time'   => substr($coupon_info['start_time'], 0, 10).'至'.substr($coupon_info['end_time'], 0, 10),
                'status' => '可使用'
        );
    }

    public function check_coupon()
    {
        $coupon_code    = Request::getParam('coupon_code', '');
        $is_order_price = Request::getParam('is_price', 0);
        $express_price  = Request::getParam('express_price', 0);

        if (!$coupon_code) {
            return '请填写需要使用的优惠券代码';
        }

        if (!$this->user_id) {
            return '请先登录后使用优惠券';
        }

        // Liw 添加直接购买的支持
        $direct_sku_id     = Request::getParam('sku_id', 0);
        $direct_buy_num    = Request::getParam('num', 0);
        $is_direct_buy = 0;
        if($direct_sku_id) {
            if(!$direct_buy_num) {
                return "请选择商品购买数量";
            }
            $direct_sku_info  = _uri('sku', $direct_sku_id);
            if(!$direct_sku_info || $direct_sku_info['status'] != 1) {
                return "您要购买的产品已下架";
            }
            $direct_goods_info = _uri('goods', $direct_sku_info['goods_id']);
            if(!$direct_goods_info || $direct_goods_info['is_sale'] != 1) {
                return "您选择的商品已下架";
            }
            // 验证库存
            if((int)$direct_sku_info['stock'] < $direct_buy_num) {
                return "商品库存不足，已被抢光了";
            }
            $is_direct_buy = 1;
        }

        // liw add
        if($is_order_price && $is_direct_buy) {
            $general_coupon_info = coupon_helper::get_general_coupon_info($direct_goods_info['id'], $coupon_code);
            if($general_coupon_info && $general_coupon_info['type'] == 'percent') {
                $tmp_order_total_price = $direct_sku_info['price']*$direct_buy_num;
                $order_total_price = $tmp_order_total_price*$general_coupon_info['discount'];
                $coupon_price  = $tmp_order_total_price*(1-$general_coupon_info['discount']);
                return array(
                                'info'  => 'ok',
                                'price' => number_format($coupon_price, 2),
                                'order_price' => number_format($order_total_price, 2),
                );
            }
        }

        $info = coupon_helper::check_is_available($coupon_code, 1);
        if ($info['info'] != 'ok') {
            return $info['info'];
        }

        if ($info['is_all_user'] == 0 && $info['status'] > 2) {
            return '当前优惠券已经被使用了';
        }

        $filter['coupon_code'] = $coupon_code;
        $filter['`status` <=']      = 2;

        $coupon_info = _uri('coupon_relation', $filter);
        if ($coupon_info['user_id'] > 0 && $this->user_id != $coupon_info['user_id']) {
            return '您没有权限使用该优惠券！';
        }

        if (empty($coupon_info)) {
            return '您不能使用该优惠券！';
        }
        if ($is_order_price) {
            $msg = coupon_helper::check_is_limit($info['category_id'], $info['goods_id'], $info['limit_price']);
            if ($msg != 'ok') {
                return $msg;
            }
        }

        $coupon_price = $info['offer_price'];

        if ($is_order_price) {
            $price = cart_helper::get_order_total($express_price, $coupon_code);
            return array(
                'info'  => 'ok',
                'price' => number_format($coupon_price, 2),
                'order_price' => number_format($price, 2),
            );
        }

        return array('info'=>'ok', 'price'=>$coupon_price);
    }
}
?>